//
//  ZDPhotoSelectorViewController.m
//  1108图片选择器
//
//  Created by Apple on 15/11/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDPhotoSelectorViewController.h"
#import "ZDPhotoSelectorCell.h"
#import "UIImage+Extension.h"

#define ZDMaxPhotoCount 6

@interface ZDPhotoSelectorViewController () <UICollectionViewDataSource, ZDPhotoSelectorCellDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) UICollectionView *photoSelectorView;

///选中的索引
@property (nonatomic, assign) NSUInteger selectedIndex;

@end

@implementation ZDPhotoSelectorViewController

static NSString *reuseIdentifier = @"ZDPhotoSelectorCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepareUI];
}


///准备UI
- (void)prepareUI
{
    [self.view addSubview:self.photoSelectorView];
    
    //添加约束
    self.photoSelectorView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[psv]-0-|" options:0 metrics:nil views:@{@"psv": _photoSelectorView}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[psv]-0-|" options:0 metrics:nil views:@{@"psv": _photoSelectorView}]];
    
    
    //设置背景颜色
    self.photoSelectorView.backgroundColor = [UIColor clearColor];
    
    //设置layout
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.photoSelectorView.collectionViewLayout;
    
    layout.itemSize = CGSizeMake(80, 80);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    //指定数据源和代理
    self.photoSelectorView.dataSource = self;
    
    
    //注册cell
    [self.photoSelectorView registerClass:[ZDPhotoSelectorCell class] forCellWithReuseIdentifier:reuseIdentifier];
}


#pragma mark - 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photos.count < ZDMaxPhotoCount ? self.photos.count+1 : self.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZDPhotoSelectorCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    if (!cell.delegate)
    {
        cell.delegate = self;
    }
    
    cell.backgroundColor = [UIColor brownColor];
    
    if (indexPath.item < self.photos.count)
    {
        cell.image = self.photos[indexPath.item];
    }
    else
    {
        cell.image = nil;
    }
    
    return cell;
}


#pragma mark - ZDPhotoSelectorCellDelegate代理方法
- (void)photoSelectorCell:(ZDPhotoSelectorCell *)cell didClickButton:(ZDPhotoSelectorButtonType)type
{
    //根据cell取出对应的索引
    NSIndexPath *indexPath = [self.photoSelectorView indexPathForCell:cell];
    self.selectedIndex = indexPath.item;
    
    //点击添加按钮
    if (type == ZDPhotoSelectorAddButton)
    {
        //打开系统相册 选取照片
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            NSLog(@"系统相册不可用");
            
            return;
        }
        
        //打开系统相册
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        
        //指定代理
        picker.delegate = self;
        
        //modal出来
        [self presentViewController:picker animated:YES completion:nil];
    }
    else if (type == ZDPhotoSelectorRemoveButton)
    {
        [self.photos removeObjectAtIndex:self.selectedIndex];
        
        //重新刷新
        [self.photoSelectorView reloadData];
    }
    
}


#pragma mark - UINavigationControllerDelegate, UIImagePickerControllerDelegate代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo
{
//    NSLog(@"%@", image);
    
    //等比缩放图片
    UIImage *scaleImage = [image scaleImage];
    
    if (self.selectedIndex < self.photos.count)
    {
        //点击替换图片
        self.photos[self.selectedIndex] = scaleImage;
    }
    else
    {
        //点击添加图片
        [self.photos addObject:scaleImage];
    }
    
    //刷新数据
    [self.photoSelectorView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark 懒加载
- (UICollectionView *)photoSelectorView
{
    if (!_photoSelectorView)
    {
        _photoSelectorView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    }
    
    return _photoSelectorView;
}

- (NSMutableArray *)photos
{
    if (!_photos)
    {
        _photos = [NSMutableArray array];
    }
    
    return _photos;
}

@end
